<?php

/*
 * 2007-2011 PrestaShop 
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@prestashop.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
 * versions in the future. If you wish to customize PrestaShop for your
 * needs please refer to http://www.prestashop.com for more information.
 *
 *  @author PrestaShop SA <contact@prestashop.com>
 *  @copyright  2007-2011 PrestaShop SA
 *  @version  Release: $Revision: 6594 $
 *  @license    http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 *  International Registered Trademark & Property of PrestaShop SA
 */

if (!defined('_CAN_LOAD_FILES_'))
    exit;

class BlockAdvSearch extends Module {

    function __construct() {
        $this->name = 'blockadvsearch';
        $this->tab = 'front_office_features';
        $this->version = 1.0;
        $this->author = 'gpaltan';
        $this->need_instance = 0;

        parent::__construct();

        $this->displayName = $this->l('Advanced Search block');
        $this->description = $this->l('Displays a block of Advanced Search');
    }

    function install() {

        return (parent::install() AND $this->registerHook('leftColumn') AND $this->registerHook('header'));
    }

    function hookLeftColumn($params) {
        global $cookie, $smarty, $link;

        $id_lang = $cookie->id_lang;
        $colors = $this->getColorAttributesGroups($id_lang);

        $attributes = $this->getAllAttributes($id_lang);

        $categories = $this->getCategories($id_lang);
        $smarty->assign(array(
            'colors' => $colors,
            'categories' => $categories
        ));
        $this->_hookCommon($params);
        return $this->display(__FILE__, 'blockadvsearch.tpl');
    }

    function hookRightColumn($params) {
        return $this->hookLeftColumn($params);
    }

    function getContent() {
        $output = '<h2></h2>';
        if (Tools::isSubmit('')) {
            
        }
        return $output . $this->displayForm();
    }

    public function displayForm() {
        $output = '';
        return $output;
    }

    function hookHeader($params) {
        //Tools::addCSS(($this->_path) . '.css', 'all');
    }

    /**
     * Get all available attribute groups
     *
     * @param integer $id_lang Language id
     * @return array Attribute groups
     */
    public function getColorAttributesGroups($id_lang) {

        $query = 'select ag.id_attribute_group
from ps_attribute_group ag
where ag.is_color_group = 1';
        $result = Db::getInstance()->ExecuteS($query);

        $query = 'select a.id_attribute
from ps_attribute a
where a.id_attribute_group = ' . $result[0]['id_attribute_group'];
        $result = Db::getInstance()->ExecuteS($query);

        $colors = NULL;

        $colors[0]['id_attribute'] = -1;
        $colors[0]['name'] = "---";

        for ($index = 1; $index <= count($result); $index++) {
            $colors[$index]['id_attribute'] = $result[$index - 1]['id_attribute'];
            $query = 'select al.name
from ps_attribute_lang al
where al.id_attribute = ' . $result[$index - 1]['id_attribute'] . '
and al.id_lang = ' . $id_lang;
            $colorresult = Db::getInstance()->ExecuteS($query);
            $colors[$index]['name'] = $colorresult[0]['name'];
        }


        return $colors;
    }

    private function getAllAttributes($id_lang) {
        global $cookie, $smarty, $link;

        $query = 'select ag.id_attribute_group
from ps_attribute_group ag
where ag.is_color_group = 0';
        $result = Db::getInstance()->ExecuteS($query);

        $attribsname = array();
        for ($ri = 0; $ri < count($result); $ri++) {
            $id_attrib_group = $result[$ri]['id_attribute_group'];
            $id_attribs = $this->getIdAttributes($id_attrib_group, $id_lang);
            $attrib_name = $this->getAttribName($id_attrib_group, $id_lang);

            $attribs = $this->getAttribs($id_attribs, $id_lang);

            $name = $attrib_name[0]['name'];
            $attribsname[$ri]['name'] = $name;
            $attributes[$ri]['name'] = $name;
            $attributes[$ri]['id_attrib_group'] = $id_attrib_group;
            $attributes[$ri]['attrib'] = $attribs;
//            $smarty->assign(array(
//                $name => $attribs
//            ));
        }
        $smarty->assign(array(
            'attribsname' => $attribsname,
            'attributes' => $attributes
        ));
    }

    private function getIdAttributes($id_attrib_group, $id_lang) {
        $query = 'select a.id_attribute,al.name
from ps_attribute a
left join ps_attribute_lang al on a.id_attribute = al.id_attribute
where a.id_attribute_group = ' . $id_attrib_group . '
and al.id_lang = ' . $id_lang . '
order by al.name asc';
        $result = Db::getInstance()->ExecuteS($query);

        $new_result = NULL;
        $add_list = NULL;
        $sizeatt = array("TU"=>0,
            "1" => 0,
            "2" => 0,
            "3" => 0,
            "4" => 0,
            "5" => 0,
            "6" => 0,
            "S" => 0,
            "M" => 0,
            "L" => 0,
            "XL" => 0,
            "XXL" => 0,
            "36" => 0,
            "38" => 0,
            "40" => 0,
            "42" => 0);
        for ($index = 0,$new_index=0,$new_add=0; $index < count($result); $index++) {
            $name=$result[$index]['name'];
            if($this->in_array_r($name, $sizeatt))
            {
//                $new_result[$new_index]['id_attribute']=$result[$index]['id_attribute'];
//                $new_result[$new_index]['name']=$result[$index]['name'];
//                $new_index++;
                $sizeatt[$name] = $result[$index]['id_attribute'];
            }
            else
            {
                $add_list[$new_add]['id_attribute']=$result[$index]['id_attribute'];
                $add_list[$new_add]['name']=$result[$index]['name'];
                $new_add++;
            }
        }
        
        $new_index = 0;
        foreach ($sizeatt as $key => $value) {
            if($value == 0)
            {
                continue;
            }
            
            $new_result[$new_index]['id_attribute'] = $value;
            $new_result[$new_index]['name'] = $key;
            $new_index++;
            
        }
        for ($index1 = 0; $index1 < count($add_list); $index1++) {
            $new_result[$new_index]['id_attribute'] = $add_list[$index1]['id_attribute'];
            $new_result[$new_index]['name'] = $add_list[$index1]['name'];
            $new_index++;
        }
        
        return $new_result;
    }

    function in_array_r($needle, $haystack) {
        foreach ($haystack as $key => $value) {
           $item = "".$key;
            if ($item === $needle || (is_array($item) && in_array_r($needle, $item))) {
            return true;
        }
    }

    return false;
}

    private function getAttribName($id_attrib_group, $id_lang) {
        $query = 'select al.name
from ps_attribute_group_lang al
where al.id_attribute_group = ' . $id_attrib_group . ' 
      and al.id_lang = ' . $id_lang;
        $result = Db::getInstance()->ExecuteS($query);
        return $result;
    }

    private function getAttribs($id_attribs, $id_lang) {
        $attribs = NULL;

        $attribs[$index]['id_attribute'] = -1;
        $attribs[$index]['name'] = "---";
        for ($index = 1; $index <= count($id_attribs); $index++) {
            $attribs[$index]['id_attribute'] = $id_attribs[$index - 1]['id_attribute'];
            $query = 'select al.name
from ps_attribute_lang al
where al.id_attribute = ' . $id_attribs[$index - 1]['id_attribute'] . '
and al.id_lang = ' . $id_lang;
            $result = Db::getInstance()->ExecuteS($query);
            $attribs[$index]['name'] = $result[0]['name'];
        }

        return $attribs;
    }

    private function getCategories($id_lang) {
        $sql = 'select c.id_category,c.name
from ps_category_lang c
where c.id_lang = ' . $id_lang . '
order by c.name asc';

        $result = Db::getInstance()->ExecuteS($sql);

        $categories = NULL;
        $categories[0]['id_category'] = -1;
        $categories[0]['name'] = "---";
        for ($index = 1; $index <= count($result); $index++) {
            $categories[$index]['id_category'] = $result[$index - 1]['id_category'];
            $categories[$index]['name'] = $result[$index - 1]['name'];
        }

        return $categories;
    }

    /**
     * _hookAll has to be called in each hookXXX methods. This is made to avoid code duplication.
     * 
     * @param mixed $params 
     * @return void
     */
    private function _hookCommon($params) {
        global $smarty;
        $smarty->assign('ENT_QUOTES', ENT_QUOTES);
        $smarty->assign('search_ssl', (int) (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off'));

        $ajaxSearch = (int) (Configuration::get('PS_SEARCH_AJAX'));
        $smarty->assign('ajaxsearch', $ajaxSearch);

        $instantSearch = (int) (Configuration::get('PS_INSTANT_SEARCH'));
        $smarty->assign('instantsearch', $instantSearch);
        if ($ajaxSearch) {
            Tools::addCSS(_PS_CSS_DIR_ . 'jquery.autocomplete.css');
            Tools::addJS(_PS_JS_DIR_ . 'jquery/jquery.autocomplete.js');
        }
        Tools::addCSS(_THEME_CSS_DIR_ . 'product_list.css');
        Tools::addCSS(($this->_path) . 'blocksearch.css', 'all');
        return true;
    }

}
